﻿Imports System.Text
Imports System.Collections.ObjectModel
Imports System.Management.Automation.Runspaces
Imports Microsoft.PowerShell

Public Class ClassMacro

    Public Titre As String
    Public Code As String

    Public Sub New()
        Me.Titre = ""
        Me.Code = ""
    End Sub

    Public Function Execute() As String
        Return Me.RunVBScript()
    End Function

    Public Function Edit()
        FormMacro.TextBoxCode.Text = Me.Code
        FormMacro.TextBoxTitre.Text = Me.Titre
        If FormMacro.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'Inscription du nouveau code
            Me.Titre = FormMacro.TextBoxTitre.Text
            Me.Code = FormMacro.TextBoxCode.Text
            Return True
        Else
            Return False
        End If
    End Function

    Private Function RunVBScript() As String
        'On ne veut pas recapturer le code historique ni les macros 
        Commun.CaptureMacro = False
        Dim oScript As New MSScriptControl.ScriptControl()
        Dim oAutomation As New ClassAuto
        Dim oRegex As New System.Text.RegularExpressions.Regex(".*", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)
        oScript.Language = "VBScript"
        oScript.AllowUI = True
        oScript.AddObject("scpEdit", oAutomation, True)
        oScript.Timeout() = 10000
        Me.Code = System.Text.RegularExpressions.Regex.Replace(Me.Code, "wscript.echo", "scpEdit.AjouterLigne", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)
        Try
            oScript.ExecuteStatement(Me.Code)
        Catch Ex As Exception
            Return "ERREUR! Ligne " & oScript.Error.Line & " : " & oScript.Error.Description & vbCrLf & oScript.Error.Text & vbCrLf & oScript.Error.Source
        End Try
        oScript = Nothing
        oAutomation = Nothing
        Commun.CaptureMacro = True
        Return ""
    End Function

    Public Function EvalVBScript(ByVal Commande As String) As String
        Dim oScript As New MSScriptControl.ScriptControl()
        oScript.Language = "VBScript"
        oScript.AllowUI = True
        Try
            oScript.AddCode(Me.Code)
            EvalVBScript = oScript.Eval(Commande)
        Catch Ex As Exception
            MsgBox("Ligne " & oScript.Error.Line - 1 & " : " & oScript.Error.Description & vbCrLf & oScript.Error.Text & vbCrLf & oScript.Error.Source, MsgBoxStyle.Exclamation, "Erreur à l'exécution de " & Me.Titre)
            EvalVBScript = ""
        End Try
        oScript = Nothing
    End Function

    Public Function EvalCondition(ByVal oMatch As System.Text.RegularExpressions.Match, ByVal Condition As String) As Boolean
        'Détermine si l'évaluation d'un résultat d'expression régulière retourne une valeur vrai
        For iSubMatch As Integer = 0 To oMatch.Groups.Count - 1
            Condition = Condition.Replace("$" & iSubMatch.ToString("0"), oMatch.Groups(iSubMatch).Value)
        Next
        Dim oScript As New MSScriptControl.ScriptControl()
        oScript.Language = "VBScript"
        oScript.AllowUI = True
        Dim sResult As String = ""
        Try
            'oScript.AddCode(Me.Code)
            sResult = oScript.Eval(Condition)
        Catch Ex As Exception
            sResult = ""
        End Try
        oScript = Nothing
        Return sResult = "True"
    End Function
End Class
